/* ----------------------------------------------------------------------------------------------------------------------
 * DesN
 * -----------------------------------------------------------------------------------------------------------------------
 */
package pure.utils 
{
	import flash.geom.Point;
	/**
	 * 数学×几何(实用类)
	 */
	
public class DesN 
{
	
	
    public static const             TWO_PI:Number  =  2.0 * Math.PI,
	
						ONE_EIGHTY_OVER_PI:Number  =  180 / Math.PI,
						
						PI_OVER_ONE_EIGHTY:Number  =  Math.PI / 180;
	
	/**
	 * 绝对值
	 */
	static public function abs(Value:Number):Number
	{
		return (Value>0)?Value:-Value;
	}
	
	
	/**
	 * 向下取整
	 */
	static public function floor(Value:Number):Number
	{
		var number:Number = int(Value);
		return (Value > 0)?(number):((number != Value)?(number - 1):(number));
	}
	
	
	/**
	 * 向上取整
	 */
	static public function ceil(Value:Number):Number
	{
		var number:Number = int(Value);
		return (Value > 0)?((number != Value)?(number + 1):(number)):(number);
	}
	
	
	/**
	 * 最接近整数
	 */
	static public function round(Value:Number):Number
	{
		var number:Number = int(Value + ((Value > 0)?0.5: -0.5));
		return (Value > 0)?(number):((number != Value)?(number - 1):(number));
	}
	
	
	/**
	 * 返回指定数值，位于最小与最大值之间
	 */
	public static function clamp(v:Number, min:Number = 0, max:Number = 1):Number
	{
		if(v < min)    return min;
		if(v > max)    return max;
		return v;
	}

	
	/**
	 * 原点间距离
	 */
	public static function getDistance(x:Number,y:Number):Number
	{
		return Math.sqrt(x * x + y * y);
	}
	
	
	/**
	 * 返回指定范围内随机「浮点数」
	 */
	public static function getRangeNum(min:Number, max:Number) : Number
	{
		var r:Number = Math.random();
		r = (max - min) * r + min;
		return r;
	}

	
	/**
	 * 获取弧度
	 */ 
	public static function getRadian(dx:Number,dy:Number):Number
	{
		return Math.atan2(dy, dx);
	}
	
	
	/**
	 * 获取角度
	 * 
	 * [Hint] 获取角度后转换为标准角度.
	 */
	public static function getDegree(dx:Number, dy:Number):Number
	{
		var n:Number;
		
		n = Math.atan2(dy, dx) * ONE_EIGHTY_OVER_PI + 90;
		n = unwrapDegrees(n);
		return n;
	}

	
	/**
	 * 换算弧度在「-PI..PI」之间
	 */
	public static function unwrapRadian(r:Number):Number 
	{ 
		r = r % TWO_PI;
		if (r > Math.PI) 
			r -= TWO_PI; 
		if (r < -Math.PI) 
			r += TWO_PI; 
		return r; 
	} 
	
	
	/**
	 * 换算角度在「0..360」之间
	 */
	public static function unwrapDegrees(r:Number):Number
	{
		r = r % 360;
		if (r < 0) r += 360;
		return r;
	}
	
	
	/**
	 * 生成颜色值
	 * @param   Red     The red component, between 0 and 255.
	 * @param   Green   The green component, between 0 and 255.
	 * @param   Blue    The blue component, between 0 and 255.
	 * @param   Alpha   How opaque the color should be, either between 0 and 1 or 0 and 255.
	 */
	static public function makeColor(Red:uint, Green:uint, Blue:uint, Alpha:Number=1.0):uint
	{
		return (((Alpha>1)?Alpha:(Alpha * 255)) & 0xFF) << 24 | (Red & 0xFF) << 16 | (Green & 0xFF) << 8 | (Blue & 0xFF);
	}
	
	
	/**
	 * 获取分解后的颜色值
	 * @param	Color	The color you want to break into components.
	 * @param	Results	An optional parameter, allows you to use an array that already exists in memory to store the result.
	 */
	static public function getRGBA(Color:uint):Array
	{
		var results:Array = [];
		results[0] = (Color >> 16) & 0xFF;
		results[1] = (Color >> 8) & 0xFF;
		results[2] = Color & 0xFF;
		results[3] = Number((Color >> 24) & 0xFF) / 255;
		return results;
	}
	
			
	/**
	 * 解析角度生成帧
	 * @param angle
	 * @param anglePerUnit		每个转向的相差角度
	 **/
	public static function parseFrameByDegrees( angle:Number , anglePerUnit:int = 5 ):int {
		var frame:int;
		if (angle > 360 - anglePerUnit/2 || angle < anglePerUnit/2 ) {
			frame = 1;
		}
		else {
			frame = int((angle - anglePerUnit / 2) / anglePerUnit) + 2;
		}
		return frame;
	}
}

}